﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ext.Net;
using FSS.BLL;
using System.Web.Security;
using FSS.BLL.Services;
using FSS.BLL.Entities;
using System.Web.UI;
using FSS.Core.Util;

namespace FSS.Core.Controls
{
    public class BasePage : Page, IBasePage
    {
        private string _screenId = "";
        private string _formTitle = "";
        private bool _isView = false;
        private bool _isAdd = false;
        private bool _isEdit = false;
        private bool _isDelete = false;
        private bool _isPrint = false;
        private bool _isImport = false;
        private bool _isExport = false;

        public string ScreenId
        {
            get { return _screenId; }
            set { _screenId = value; }
        }
        public string FormTitle
        {
            get { return _formTitle; }
            set { _formTitle = value; }
        }
        public bool IsView
        {
            get { return _isView; }
            set { _isView = value; }
        }
        public bool IsAdd
        {
            get { return _isAdd; }
            set { _isAdd = value; }
        }
        public bool IsEdit
        {
            get { return _isEdit; }
            set { _isEdit = value; }
        }
        public bool IsDelete
        {
            get { return _isDelete; }
            set { _isDelete = value; }
        }
        public bool IsPrint
        {
            get { return _isPrint; }
            set { _isPrint = value; }
        }
        public bool IsImport
        {
            get { return _isImport; }
            set { _isImport = value; }
        }
        public bool IsExport
        {
            get { return _isExport; }
            set { _isExport = value; }
        }
        public BasePage(string screenId)
        {
            this._screenId = screenId;
            CheckPermission();

            //6ECFE9FF08ABA25FBFF
            //string MachineID = License.GetMachineID("D");


        }

        public static void RedirectPermission(string screenId)
        {

            //string username = Membership.GetUser().UserName;
            //RolesService rolesS = RolesService.GetInstance();
            //PermissionService permissionS = PermissionService.GetInstance();
            //RolesEntity rolesE = new RolesEntity();
            //rolesE.username = username;
            //IList<RolesEntity> listRole = rolesS.getAllRolesByUserName(rolesE);
            //foreach (RolesEntity _rolesE in listRole)
            //{

            //    PermissionEntity PermissionE = permissionS.getAllPermissionByRoleID_ScreenId(_rolesE.roleid, screenId);
            //    if (!PermissionE.pview)
            //    {
            //        FormsAuthentication.RedirectToLoginPage();
            //    }
            //}
            //if (!Roles.IsUserInRole(Membership.GetUser().UserName, strRole))
            //{
            //    FormsAuthentication.RedirectToLoginPage();
            //}
        }

        public void CheckPermissionControl()
        {
            Ext.Net.Button btnAdd = (Ext.Net.Button)FindControl("btnAdd");
            Ext.Net.Button btnEdit = (Ext.Net.Button)FindControl("btnEdit");
            Ext.Net.Button btnDelete = (Ext.Net.Button)FindControl("btnDelete");
            Ext.Net.Button btnPrint = (Ext.Net.Button)FindControl("btnPrint");
            Ext.Net.Button btnExport = (Ext.Net.Button)FindControl("btnExport");
            Ext.Net.Button btnImport = (Ext.Net.Button)FindControl("btnImport");
            Ext.Net.GridPanel grdMain = (Ext.Net.GridPanel)FindControl("grdMain");
            if (null != btnAdd)
            {
                btnAdd.Visible = this.IsAdd;
            }
            if (null != btnEdit)
            {
                btnEdit.Visible = this.IsEdit;
            }
            if (null != btnDelete)
            {
                btnDelete.Visible = this.IsDelete;
            }
            if (null != btnPrint)
            {
                btnPrint.Visible = this.IsPrint;
            }
            if (null != btnExport)
            {
                btnExport.Visible = this.IsExport;
            }
            if (null != btnImport)
            {
                btnImport.Visible = this.IsImport;
            }
            if (null != grdMain)
            {
                int columnNumber = grdMain.ColumnModel.Columns.Count;
                int colEditIndex = columnNumber - 2;
                int colDelIndex = columnNumber - 1;
                grdMain.ColumnModel.SetHidden(colEditIndex, !this.IsEdit);
                grdMain.ColumnModel.SetHidden(colDelIndex, !this.IsDelete);
            }
        }

        private void CheckPermission()
        {
            if (User.Identity == null || !User.Identity.IsAuthenticated)
            {
                FormsAuthentication.SignOut();
                FormsAuthentication.RedirectToLoginPage();
                return;
            }
            if (this.ScreenId.Equals(SCREENS.MAIN))
            {
                return;
            }
            this._isView = false;
            this._isAdd = false;
            this._isEdit = false;
            this._isDelete = false;
            this._isPrint = false;
            this._isImport = false;
            this._isExport = false;
            int RoleId = -1;

            if (FSSContext.Current.User != null)
            {
                RoleId = FSSContext.Current.User.RoleId;
                int brancdId = FSSContext.Current.BranchId;
            }

            ////chi dung thi test chuc nang 
            //if (FSSContext.Current.User.UserName.Equals("admin"))
            //{
            //    this._isView = this._isAdd = this._isEdit = this._isDelete = this._isImport = this._isExport = true;
            //    return;
            //}
            if (RoleId <= 0)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }
            RoleService roleService = RoleService.GetInstance();
            PermissionService permissionService = PermissionService.GetInstance();
            PermissionEntity permissionEntity = new PermissionEntity();
            permissionEntity.RoleId = RoleId;
            permissionEntity.ScreenId = this.ScreenId;
            permissionEntity = permissionService.LoadByPrimaryKey(permissionEntity);
            if (permissionEntity == null || !permissionEntity.IsView)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }

            this._isView = permissionEntity.IsView;
            this._isAdd = permissionEntity.IsAdd;
            this._isEdit = permissionEntity.IsEdit;
            this._isDelete = permissionEntity.IsDelete;
            this._isPrint = permissionEntity.IsPrint;
            this._isImport = permissionEntity.IsImport;
            this._isExport = permissionEntity.IsExport;
            return;

            //try
            //{
            //    foreach (RolesEntity _rolesE in listRole)
            //    {
            //        if ("administrator".Equals(_rolesE.role_name.ToLower()))
            //        {
            //            this._isView = true;
            //            this._isAdd = true;
            //            this._isEdit = true;
            //            this._isDelete = true;
            //            this._isPrint = true;
            //        }
            //        permissionE = permissionS.getAllPermissionByRoleID_ScreenId(_rolesE.roleid, this._screenId);
            //        if (permissionE != null)
            //            this._formTitle = permissionE.screen_name;
            //        return;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    return;
            //}
            //if (null != permissionE)
            //{
            //    if (!permissionE.pview)
            //    {
            //        FormsAuthentication.RedirectToLoginPage();
            //    }
            //    this._isView = permissionE.pview;
            //    this._isAdd = permissionE.padd;
            //    this._isEdit = permissionE.pedit;
            //    this._isDelete = permissionE.pdelete;
            //    this._isPrint = permissionE.pprint;
            //    this._formTitle = permissionE.screen_name;
            //}

        }
        public static void checkPermissionByScreen(string screen_id, ref bool _isView, ref bool _isAdd, ref bool _isEdit, ref bool _isDelete, ref bool _isPrint, ref string _formTitle)
        {

            //string username = Membership.GetUser().UserName;
            //RolesService rolesS = RolesService.GetInstance();
            //PermissionService permissionS = PermissionService.GetInstance();
            //RolesEntity rolesE = new RolesEntity();
            //rolesE.username = username;
            //IList<RolesEntity> listRole = rolesS.getAllRolesByUserName(rolesE);
            //PermissionEntity permissionE = new PermissionEntity();
            //foreach (RolesEntity _rolesE in listRole)
            //{
            //    if ("administrator".Equals(_rolesE.role_name.ToLower()))
            //    {
            //        _isView = true;
            //        _isAdd = true;
            //        _isEdit = true;
            //        _isDelete = true;
            //        _isPrint = true;

            //    }
            //    permissionE = permissionS.getAllPermissionByRoleID_ScreenId(_rolesE.roleid, screen_id);
            //    if (permissionE != null)
            //        _formTitle = permissionE.screen_name;
            //    return;
            //}
            //if (null != permissionE)
            //{
            //    if (!permissionE.pview)
            //    {
            //        FormsAuthentication.RedirectToLoginPage();
            //    }
            //    _isView = permissionE.pview;
            //    _isAdd = permissionE.padd;
            //    _isEdit = permissionE.pedit;
            //    _isDelete = permissionE.pdelete;
            //    _isPrint = permissionE.pprint;
            //    _formTitle = permissionE.screen_name;
            //}

        }
        public void Notification(string errorCode)
        {

            string title = "Thông báo";
            string message = "";
            switch (errorCode)
            {
                case "0x000001": message = "Không tìm thấy Thư mục Sao lưu"; break;
                case "0x000002": message = "Không tìm thấy files"; break;
                default: message = errorCode; break;
            }
            Ext.Net.Notification.Show(new NotificationConfig
            {
                Title = title,
                ShowPin = true,
                Html = message
            });
        }

    }
}
